<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2014/10/15
 * Time: 17:42
 * 用户组管理
 */
class GroupController extends AdminController{
    /**
     * 用户组列表
     */
    public function actionList()
    {
        $group_name = $this->get('group_name');
        $p = $this->get('p', 1);
        $criteria = new CDbCriteria();
        if ($group_name) 
        {
            $criteria->addSearchCondition('group_name', $group_name);
        }
        $criteria->addCondition('status = 1');
        $total = ManageGroup::model()->count($criteria);
        $criteria->limit = $this->limit;
        $criteria->offset = ($p -1) * $this->limit;
        $data = ManageGroup::model()->findAll($criteria);
        $page = new Page($total, $this->limit);
        $this->smarty->assign('page', $page->style1());
        $this->smarty->assign('group_name', $group_name);
        $this->smarty->assign('list', $data);
        $auth = require Yii::app()->basePath.'/config/auth.php';
        $this->smarty->assign('auth', $auth);
        
        $this->smarty->display('group/list.tpl');
    }

    /**
     * 添加用户组
     */
    public function actionAdd()
    {
        $auth = require Yii::app()->basePath.'/config/auth.php';
        $this->smarty->assign('auth', $auth);
        $this->smarty->assign('op_name','添加用户组');
        $this->smarty->display('group/add.tpl');
    }

    /**
     * 添加
     */
    public function actionDoAdd(){
        $group = new ManageGroup();
        $group->group_name = $this->post('group_name');
        $group->group_describe = $this->post('group_describe');
        $group_auth = $this->post('group_auth');
        $selall = $this->post('selall');
        if ($selall == '*') {
            $group->group_auth = '*';
        } else {
            $group_auth[] = 'index.index';
            $group->group_auth = join(',', $group_auth);
        }
        
        $group->create_time = time();
        $group->update_time = time();
        $group->status = $this->post('status');
        $res = $group->save();
        if ($res) {
            $this->jump('保存成功', '/webadmin/group/list');
        } else {
            $this->jump('保存失败');
        }
    }

    /**
     * 编辑用户组
     */
    public function actionEdit(){
        $auth = require Yii::app()->basePath.'/config/auth.php';
        $this->smarty->assign('auth', $auth);
        $group_id = $this->get('group_id');
        if ($group_id) {
            $group = new ManageGroup();
            $info = $group->findByPk($group_id)->attributes;
        }
        $this->smarty->assign('op_name','编辑用户组');
        $this->smarty->assign('info', $info);
        $this->smarty->display('group/add.tpl');
    }

    /**
     * 添加
     */
    public function actionDoEdit(){
        $data['group_name'] = $this->post('group_name');
        $data['group_describe'] = $this->post('group_describe');
        $group_auth = $this->post('group_auth');
        $selall = $this->post('selall');
        if ($selall == '*') {
            $data['group_auth'] = '*';
        } else {
            $group_auth[] = 'index.index';
            $data['group_auth'] = join(',', $group_auth);
        }
        $data['update_time'] = time();
        $data['status'] = $this->post('status');
        $group_id = $this->post('group_id');
        $group = new ManageGroup();
        $res = $group->updateByPk($group_id, $data);
        if ($res) {
            $this->jump('保存成功', '/webadmin/group/list');
        } else {
            $this->jump('保存失败');
        }
    }
    /***
     * 删除用户组
     */
    public function actionDel(){
        $id = (int)$this->get('group_id');
        $group = new ManageGroup();
        if ($group->updateByPk($id, array('status' => 0))){
            $this->_ajax(array('status' => 1,'note' => '成功'));
        } else {
            $this->_ajax(array('status' => 0,'note' => '失败'));
        }
    }
    /**
     * 权限列表
     */
    public function actionAuth(){
        $group_id = $this->get('group_id');
        $auth = include(Yii::app()->basePath.'/config/auth.php');
        $this->smarty->assign('auth', $auth);
        $this->smarty->assign('group_id', $group_id);
        $this->smarty->display('group/auth.tpl');
    }
    /**
     * 分配权限
     */
    public function actionDoauth(){
        $auth = $this->post('auth');
        $data['group_auth'] = join(',', $auth);
        $group_id = (int)$this->post('group_id');
        $data['update_time'] = time();
        $group = new ManageGroup();
        $res = $group->updateByPk($group_id, $data);
        if ($res) {
            $this->jump('保存成功', '/webadmin/group/list');
        } else {
            $this->jump('保存失败');
        }
    }
}